{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#方法一，使用Python内置的time模块，及time.time and time.clock function\n",
    "import time\n",
    "start=time.time()\n",
    "for i in range(iteration):\n",
    "    #被测试的代码\n",
    "    #……\n",
    "    elapsed_per=(time.time()-start)/iteration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.00150001049042\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import pandas as pd\n",
    "start=time.time()\n",
    "for i in range(10):\n",
    "    data=pd.read_csv('c:\\\\pythoncoding\\\\S1EP3_Iris.txt')#测试pandas读取文件的速度\n",
    "    elapsed_per=(time.time()-start)/10\n",
    "print elapsed_per"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 172 ms\n",
      "Wall time: 84 ms\n"
     ]
    }
   ],
   "source": [
    "#方法二 使用magic command %time及%timeit\n",
    "strings=['foo','abc','python','hello world','j=va','cool']*100000\n",
    "method1=[x for x in strings if x.startswith('foo')]\n",
    "method2=[x for x in strings if x[:3]=='foo']\n",
    "%time method1=[x for x in strings if x.startswith('foo')]\n",
    "%time method2=[x for x in strings if x[:3]=='foo']\n",
    "# %time是执行一句的时间，不能写成%time method1，这不是测试的method1=[x for x in strings if x.startswith('foo')]的时间，而是输出method1的时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 loops, best of 3: 139 ms per loop\n",
      "10 loops, best of 3: 52.4 ms per loop\n"
     ]
    }
   ],
   "source": [
    "%timeit [x for x in strings if x.startswith('foo')]\n",
    "%timeit [x for x in strings if x[:3]=='foo']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The slowest run took 4.08 times longer than the fastest. This could mean that an intermediate result is being cached \n",
      "100000 loops, best of 3: 9.29 µs per loop\n",
      "100000 loops, best of 3: 13.3 µs per loop\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr=np.arange(100000).reshape(1000,100)\n",
    "%timeit arry1=arr[[1,7,5,999,94,34,6,33,77,44,32,78,64]][:,[2,3,8,55,34,88,21,34,98]]\n",
    "%timeit arr2=arr[np.ix_([1,7,5,999,94,34,6,33,77,44,32,78,64],[2,3,8,55,34,88,21,34,98])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
